package com.raccoonagents.tools.cascet2d.util;

import java.util.Iterator;

import com.raccoonagents.tools.cascet2d.interfaces.IGraphicContainer;
import com.raccoonagents.tools.cascet2d.interfaces.IRenderableStageElement;

public class ElementUtils {
	/**
	 * method to automatically update relations if they are changed (a child is moved away from a parent and assigned
	 * as a child for some other parent)
	 * @param container
	 */
	public static void updateContainerRelations(final IGraphicContainer container){
		final Iterator<IRenderableStageElement> i = container.getChildren().iterator();
		while(i.hasNext()){
			final IRenderableStageElement e = i.next();			
			// if there's wrongly assigned element,
			if(e.getParentElement() != container){
				FWLogger.info("a parent for child ("+e.toString()+") was changed, updating automatically...");
				// assign as children for it's parent
				if(IGraphicContainer.class.isAssignableFrom(e.getParentElement().getClass())){
					final IGraphicContainer c = (IGraphicContainer) e.getParentElement();
					c.addChild(e);				
					FWLogger.info(e.toString() + " is now child of " + c.toString());					
				}
				
				i.remove();
			}
		}
	}

	}
	

